package pers.litao.bit.order.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import pers.litao.bit.commons.entities.order.DistributionEntity;
import pers.litao.bit.commons.utils.*;
import pers.litao.bit.commons.vo.order.AddressPointVo;
import pers.litao.bit.order.dao.DistributionMapper;
import pers.litao.bit.order.service.DistributionService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李涛
 * @date 2020-12-08 14:11
 */
@Service
@Slf4j
public class DistributionServiceImpl extends ServiceImpl<DistributionMapper, DistributionEntity> implements DistributionService {
    /**
     * 查看合并的配送订单
     *
     * @param current
     * @return
     */
    @Override
    public R getList(Long current, LoginUserInfo loginUserInfo) {

        // 用户未登录
        if(null == loginUserInfo){
            log.error("用户未登录操作" + SysUtil.getClientInfo().buildInfo());
            return R.error(Constant.ResponseCode.TOKEM_INVALID.getCode(),
                    Constant.ResponseCode.TOKEM_INVALID.getMsg(), null);
        }
        Page<DistributionEntity> page = new Page<>();
        // 设置分页参数
        page.setCurrent(current);
        page.setSize(Constant.DISTRIBUTION_SIZE);
        this.baseMapper.selectPage(page, new QueryWrapper<DistributionEntity>()
                .eq("user_id", loginUserInfo.getUserId())
                .orderByDesc("create_time"));
        List<DistributionEntity> collect = page.getRecords().stream()
                .peek(m -> {
                    m.setOrdersList(JSONUtil.toList(JSONUtil.parseArray(m.getOrders()), Long.class));
                    m.setPointsList(JSONUtil.toList(JSONUtil.parseArray(m.getPoints()), AddressPointVo.class));
                }).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        map.put("orderData", collect);
        map.put("size", page.getTotal());
        return R.ok(map);
    }

    /**
     * 单个订单点击配送完成
     *
     * @param id
     * @return
     */
    @Override
    public R overOrder(Long id,LoginUserInfo loginUserInfo) {
        // 用户未登录
        if(null == loginUserInfo){
            log.error("用户未登录操作" + SysUtil.getClientInfo().buildInfo());
            return R.error(Constant.ResponseCode.TOKEM_INVALID.getCode(),
                    Constant.ResponseCode.TOKEM_INVALID.getMsg(), null);
        }
        DistributionEntity distributionEntity = this.baseMapper.selectById(id);
        // 用户身份不一致
        if(distributionEntity == null || !distributionEntity.getUserId().equals(loginUserInfo.getUserId())){
            log.error("配送订单所属供应商与请求身份不一致" + loginUserInfo.info() + SysUtil.getClientInfo().buildInfo());
            return R.error(Constant.ResponseCode.ILLEGAL_OPERATION.getCode(),
                    Constant.ResponseCode.ILLEGAL_OPERATION.getMsg(),null);
        }
        distributionEntity.setStatus(1);
        this.baseMapper.updateById(distributionEntity);
        log.info("ID为"+id+"的配送订单完成" + loginUserInfo.info() + SysUtil.getClientInfo().buildInfo());
        return R.ok();
    }
}
